<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Convert a phar archive to another executable phar archive file format</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="phar.converttodata.html">« Phar::convertToData</a></li>
      <li style="float: right;"><a href="phar.copy.html">Phar::copy »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.phar.html">Phar</a></li>
    <li>Convert a phar archive to another executable phar archive file format</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="phar.converttoexecutable" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Phar::convertToExecutable</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::convertToExecutable</span> &mdash; <span class="dc-title">Convert a phar archive to another executable phar archive file format</span></p>

 </div>


 <div class="refsect1 description" id="refsect1-phar.converttoexecutable-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Phar::convertToExecutable</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$format</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$compression</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$extension</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">?</span><span class="type"><a href="class.phar.html" class="type Phar">Phar</a></span></span></div>

  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">此方法需要
将 <var class="filename">php.ini</var> 中的 <code class="literal">phar.readonly</code> 设为
<code class="literal">0</code> 以适合 <span class="classname"><a href="class.phar.html" class="classname">Phar</a></span>
对象.  否则, 将抛出<span class="classname"><a href="class.pharexception.html" class="classname">PharException</a></span>.</p></p></blockquote>
  <p class="para">
   This method is used to convert a phar archive to another file format.  For instance,
   it can be used to create a tar-based executable phar archive from a zip-based
   executable phar archive, or from an executable phar archive in the phar file format.  In
   addition, it can be used to apply whole-archive compression to a tar or phar-based
   archive.
  </p>
  <p class="para">
   If no changes are specified, this method throws a <span class="classname"><a href="class.badmethodcallexception.html" class="classname">BadMethodCallException</a></span>.
  </p>
  <p class="para">
   If successful, the method creates a new archive on disk and returns a <span class="classname"><a href="class.phar.html" class="classname">Phar</a></span>
   object.  The old archive is not removed from disk, and should be done manually after
   the process has finished.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-phar.converttoexecutable-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
       This should be one of <code class="literal">Phar::PHAR</code>, <code class="literal">Phar::TAR</code>,
       or <code class="literal">Phar::ZIP</code>.  If set to <strong><code>null</code></strong>, the existing file format
       will be preserved.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">compression</code></dt>

     <dd>

      <p class="para">
       This should be one of <code class="literal">Phar::NONE</code> for no whole-archive
       compression, <code class="literal">Phar::GZ</code> for zlib-based compression, and
       <code class="literal">Phar::BZ2</code> for bzip-based compression.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">extension</code></dt>

     <dd>

      <p class="para">
       This parameter is used to override the default file extension for a
       converted archive.  Note that all zip- and tar-based phar archives must contain
       <code class="literal">.phar</code> in their file extension in order to be processed as a
       phar archive.
      </p>
      <p class="para">
       If converting to a phar-based archive, the default extensions are
       <code class="literal">.phar</code>, <code class="literal">.phar.gz</code>, or <code class="literal">.phar.bz2</code>
       depending on the specified compression.  For tar-based phar archives, the
       default extensions are <code class="literal">.phar.tar</code>, <code class="literal">.phar.tar.gz</code>,
       and <code class="literal">.phar.tar.bz2</code>.  For zip-based phar archives, the
       default extension is <code class="literal">.phar.zip</code>.
      </p>
     </dd>

    
   </dl>

  </p>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-phar.converttoexecutable-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The method returns a <span class="classname"><a href="class.phar.html" class="classname">Phar</a></span> object on success,
   or <strong><code>null</code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-phar.converttoexecutable-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   This method throws <span class="classname"><a href="class.badmethodcallexception.html" class="classname">BadMethodCallException</a></span> when unable
   to compress, an unknown compression method has been specified, the requested
   archive is buffering with <span class="function"><a href="phar.startbuffering.html" class="function">Phar::startBuffering()</a></span> and
   has not concluded with <span class="function"><a href="phar.stopbuffering.html" class="function">Phar::stopBuffering()</a></span>, an
   <span class="classname"><a href="class.unexpectedvalueexception.html" class="classname">UnexpectedValueException</a></span> if write support is disabled,
   and a <span class="classname"><a href="class.pharexception.html" class="classname">PharException</a></span> if any problems are encountered
   during the phar creation process.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-phar.converttoexecutable-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">format</code>, <code class="parameter">compression</code>, and <code class="parameter">localName</code> are now nullable.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-phar.converttoexecutable-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-727">
    <p><strong>示例 #1 A <span class="function"><strong>Phar::convertToExecutable()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     Using Phar::convertToExecutable():
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tarphar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'myphar.phar.tar'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;convert&nbsp;it&nbsp;to&nbsp;the&nbsp;phar&nbsp;file&nbsp;format<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;note&nbsp;that&nbsp;myphar.phar.tar&nbsp;is&nbsp;*not*&nbsp;unlinked<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$tarphar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">convertToExecutable</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">PHAR</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;creates&nbsp;myphar.phar<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createDefaultStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'cli.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'web/index.php'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;creates&nbsp;myphar.phar.tgz<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$compressed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">convertToExecutable</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">TAR</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">GZ</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'.phar.tgz'</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;handle&nbsp;the&nbsp;error&nbsp;here<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-phar.converttoexecutable-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="phar.converttodata.html" class="function" rel="rdfs-seeAlso">Phar::convertToData()</a> - Convert a phar archive to a non-executable tar or zip file</span></li>
    <li class="member"><span class="function"><a href="phardata.converttoexecutable.html" class="function" rel="rdfs-seeAlso">PharData::convertToExecutable()</a> - Convert a non-executable tar/zip archive to an executable phar archive</span></li>
    <li class="member"><span class="function"><a href="phardata.converttodata.html" class="function" rel="rdfs-seeAlso">PharData::convertToData()</a> - Convert a phar archive to a non-executable tar or zip file</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>